/*-+-*****************************************************************************
 Copyright (C) 2009-2012 Oceanside Software Corporation (R)
 Dallas, Texas

 Author       : Jason T. Brower
 Purpose      : This file is part of a point-of-sale software package
                originally written for Table-Service Restaurants.
              
 Date         : July 8th of 2012
              
 History      : The application that this software file belongs to was 
                written over a 3-Year period.  The first version was written
                in C# .Net 3.5 using Windows Forms.  Being new to .Net at
                the time, I soon learned of Windows Presentation Foundation,
                liked the graphics capabilities and databinding so I
                migrated to WPF.  When the .Net 4.0 framework was released
                the application was migrated from 3.5 to 4.0. Now today,
                .Net 4.5 RC is out and I was already in process of migrating
                before I made this public.
                
Excuse Me     : Please excuse the coding style, it is not representative of
                my typical work.  During the three years I was under a very
                tight budget, constantly running out of funding and was 
                working 18 hour days hyped up on 5-Hour Energy.  The 
                design is not textbook.  It is monolythic because of
                previous issues I will not get into.  There are few comments
                in the code and the ones that are there may be incorrect.
                
What it is not: This is not the exact version that I took through PA-DSS 2.0
                nor is it the exact version running at my customer sites.
                I removed some details for security purposes including some
                encryption algorithms.  The reporting was removed because
                I was including a third party library that cannot be 
                published with this Open Source.
                
What it is    : This application has been running at high volume restaraunts
                for over a year.  It never freezes, crashes or does anything
                unexpected.  So long as it is built, installed and configured
                correctly, it is a strong system that we can all enhance.

Contact       : Please note that I can no longer work for free.  However, you
                can reach me at information@oceansidesw.com.                                                      

*********************************************************************************/
using System;
using System.Linq;

#if _LOCALDB
  using Oceanside.LocalDB;
#else
using Oceanside.RemoteDB;
#endif

#if _LOCALDB
  namespace Oceanside.LocalDB
#else
namespace Oceanside.RemoteDB
#endif
{

    /// <summary>
    /// A menu of product groups.
    /// </summary>
    public partial class MENU
    {
        #region Public Static Functions

        private static MENU _cachedMenu = null;
        public static MENU CachedMenu
        {
            get { return MENU._cachedMenu; }
            set { MENU._cachedMenu = value; }
        }

        /// <summary>
        /// 
        /// 
        /// </summary>
        public static void CacheCurrentMenuInstance()
        {
            MENU currentMenu = null;
            MENU_GROUP mg = null;
            KIOSK kiosk = KIOSK.Get(SHIFT.GlobalContext);
            try
            {
                if (kiosk != null && kiosk.MenuGroupId != null)
                {
                    var q = from MENU_GROUP in SHIFT.GlobalContext.MENU_GROUPS.Include("MENUS")
                            where MENU_GROUP.Id == kiosk.MenuGroupId &&
                            MENU_GROUP.IsDeleted == false
                            select MENU_GROUP;

                    if (q.Any())
                    {
                        mg = q.First();

                    }
                }
                int count = 0;

                if (mg != null)
                {
                    if (mg.MENUS != null)
                    {
                        count = mg.MENUS.Count();
                    }
                }
                if (count == 1)
                {
                    currentMenu = mg.MENUS.First();
                }
                else if (count != 0)
                {
                    DateTime rightNow = DateTime.Now;
                    foreach (MENU m in mg.MENUS)
                    {
                        if (!m.IsDeleted && Functions.IsInBetweenTimeFrame(rightNow, m.StartTime, m.EndTime) && PageMenusImp.CanRunMenuToday(m))
                        {
                            currentMenu = m;
                            break;
                        }
                    }

                    if (currentMenu == null)
                    {
                        foreach (MENU m in mg.MENUS)
                        {
                            if (!m.IsDeleted && PageMenusImp.CanRunMenuToday(m))
                            {
                                currentMenu = m;
                                break;
                            }
                        }

                        ///Last Try
                        if (currentMenu == null)
                        {
                            var q = from M in mg.MENUS where M.IsDeleted == false select M;
                            if (q.Any())
                            {
                                currentMenu = q.First();
                            }
                        }
                    }
                }
            }
            catch (Exception e)
            {
                Logging.LogException(e);
            }
            _cachedMenu = currentMenu;
        }

        /// <summary>
        /// Will return the main categories for the active meal group.
        /// </summary>
        /// <returns>MainCategories</returns>
        public static PRODUCT_GROUP[] GetMainCategories(int pivotIndx)
        {
            if (CachedMenu == null)
            {
                return null;
            }
            else
            {
                if (CachedMenu.OrderMenuItemsByWeight == true)
                {
                    var q = from pg in CachedMenu.PRODUCT_GROUPS.Where(p => !p.IsDeleted).OrderByDescending(p => p.MenuPosition)
                                .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select pg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                    else
                    {
                        return null;
                    }
                }
                else
                {
                    var q = from pg in CachedMenu.PRODUCT_GROUPS.Where(p => !p.IsDeleted).OrderBy(p => p.Name)
                                .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select pg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                    else
                    {
                        return null;
                    }
                }
            }
        }

        /// <summary>
        /// Returns the count of modifiers or subcategories for the selected
        /// item depending on whether we are in modifier mod or sub menu mode.
        /// </summary>
        /// <returns>Count as an int.</returns>
        public static int GetMainCategoryCount()
        {
            MENU menu = CachedMenu;
            if (menu != null)
            {
                var q = menu.PRODUCT_GROUPS.Where(p => !p.IsDeleted);
                if (q.Any())
                {
                    return q.Count();
                }
            }
            return 0;
        }

        /// <summary>
        /// Return the numTimes of main modifier groups associated with the passed in subcategory.
        /// </summary>
        /// <param Name="selectedSubCat"></param>
        /// <returns></returns>
        public static int GetForcedModifierGroupCount(PRODUCT selectedSubCat)
        {
            if (selectedSubCat != null)
            {
                var q = selectedSubCat.FORCED_MODIFIER_GROUPS.Where(p => !p.IsDeleted);
                if (q.Any())
                {
                    return q.Count();
                }
            }
            return 0;
        }

        /// <summary>
        /// Gets the sub categories for the current state.  If we are in modifier mode then
        /// modifiers for the currently selected sub menu item will be returned.
        /// </summary>
        /// <param Name="pivotIndx">Start point from where cats should be taken.</param>
        /// <returns>List PosDb.Items</returns>
        public static PRODUCT[] GetProductsInView(PRODUCT_GROUP selectedMainCat, int pivotIndx)
        {
            if (selectedMainCat != null)
            {
                if (selectedMainCat.OrderMenuItemsByWeight == true)
                {
                    var q = from prods in selectedMainCat.PRODUCTS.Where(p => !p.IsDeleted)
                            .OrderByDescending(p => p.MenuPosition)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            where (prods.RemoveAtZeroCount == false ||
                            (prods.RemoveAtZeroCount == true && prods.Downtick > 0))
                            select prods;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from prods in selectedMainCat.PRODUCTS.Where(p => !p.IsDeleted)
                            .OrderBy(p => p.Name)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            where (prods.RemoveAtZeroCount == false ||
                            (prods.RemoveAtZeroCount == true && prods.Downtick > 0))
                            select prods;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// Returns the count of modifiers or subcategories for the selected
        /// item depending on whether we are in modifier mod or sub menu mode.
        /// </summary>
        /// <returns>Count as an int.</returns>
        public static int GetSubCategoryCount(PRODUCT_GROUP selectedMainCat)
        {
            var q = selectedMainCat.PRODUCTS.Where(p => !p.IsDeleted);
            if (q.Any())
            {
                return q.Count();
            }
            return 0;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="pivotIndx"></param>
        /// <returns></returns>
        public static FORCED_MODIFIER_GROUP[] GetForcedModifierGroupsInView(PRODUCT selectedProduct, int pivotIndx)
        {
            if (null != selectedProduct)
            {
                if (CachedMenu.OrderMenuItemsByWeight == true)
                {
                    var q = from fmg in selectedProduct.FORCED_MODIFIER_GROUPS.Where(p => !p.IsDeleted).OrderByDescending(p => p.MenuPosition)
                                .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select fmg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from fmg in selectedProduct.FORCED_MODIFIER_GROUPS.Where(p => !p.IsDeleted).OrderBy(p => p.Name)
                                .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select fmg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="pivotIndx"></param>
        /// <returns></returns>
        public static EXCEPTION_MODIFIER_GROUP[] GetExceptionModifierGroupsInView(PRODUCT_GROUP pg, int pivotIndx)
        {
            if (null != pg)
            {
                if (CachedMenu.OrderMenuItemsByWeight == true)
                {
                    var q = from emg in pg.EXCEPTION_MODIFIER_GROUPS.Where(p => !p.IsDeleted).OrderByDescending(p => p.MenuPosition)
                                .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select emg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from emg in pg.EXCEPTION_MODIFIER_GROUPS.Where(p => !p.IsDeleted).OrderBy(p => p.Name)
                        .Skip(pivotIndx).Take(Oceanside.Constants.CONST_ROWS_PER_VIEW)
                            select emg;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="pivotIndx"></param>
        /// <returns></returns>
        public static FORCED_MODIFIER_GROUP[] GetAllForcedModifierGroups(PRODUCT selectedProduct)
        {
            if (null != selectedProduct)
            {
                MENU menu = CachedMenu;

                if (menu.OrderMenuItemsByWeight == true)
                {
                    var q = from FORCED_MODIFIER_GROUP in selectedProduct.FORCED_MODIFIER_GROUPS.Where(p => !p.IsDeleted)
                            orderby FORCED_MODIFIER_GROUP.MenuPosition descending
                            select FORCED_MODIFIER_GROUP;

                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from FORCED_MODIFIER_GROUP in selectedProduct.FORCED_MODIFIER_GROUPS.Where(p => !p.IsDeleted)
                            orderby FORCED_MODIFIER_GROUP.Name descending
                            select FORCED_MODIFIER_GROUP;

                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="selectedMainModifier"></param>
        /// <param Name="pivotIndx"></param>
        /// <returns></returns>
        public static FORCED_MODIFIER[] GetForcedModifiersInView(FORCED_MODIFIER_GROUP selectedMainModifier, int pivotIndx)
        {
            if (selectedMainModifier != null)
            {
                if (selectedMainModifier.OrderMenuItemsByWeight == true)
                {
                    var q = from prods in selectedMainModifier.FORCED_MODIFIERS.Where(p => !p.IsDeleted).OrderByDescending(p => p.MenuPosition)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            select prods;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from prods in selectedMainModifier.FORCED_MODIFIERS.Where(p => !p.IsDeleted).OrderBy(p => p.Name)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            select prods;
                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param Name="selectedExceptionMainModifier"></param>
        /// <param Name="subPivotIndex"></param>
        /// <returns></returns>
        public static EXCEPTION_MODIFIER[] GetExceptionModifiersInView(EXCEPTION_MODIFIER_GROUP selected, int pivotIndx)
        {
            if (selected != null)
            {
                if (selected.OrderMenuItemsByWeight == true)
                {
                    var q = from prods in selected.EXCEPTION_MODIFIERS.Where(p => !p.IsDeleted).OrderByDescending(p => p.MenuPosition)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            select prods;

                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
                else
                {
                    var q = from prods in selected.EXCEPTION_MODIFIERS.Where(p => !p.IsDeleted).OrderBy(p => p.Name)
                            .Skip(pivotIndx).Take(Oceanside.Constants.CONST_BUTTN_PER_SUBVIEW)
                            select prods;

                    if (q.Any())
                    {
                        return q.ToArray();
                    }
                }
            }
            return null;
        }
        #endregion Public Static Functions
    }
}
